class StageScroll extends egret.DisplayObjectContainer {
	
	private stageW: number;
	private stageH: number;

	private scrollImgTexture: egret.Texture;
	private scrollImgTextureWidth: number;
	private scrollImgtextureHeight: number;

	private imgCount: number;
	private arrScrollImg: egret.Bitmap[];
	private stageScrollSpeed: number = 1.5;

	public constructor() {
		super();
		// this.createGameScene();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
	}

	private onAddToStage(et: egret.Event) {
		this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
		this.createGameScene();
	}

	private createGameScene() {
		//
		this.stageW = this.stage.stageWidth;
		this.stageH = this.stage.stageHeight;

		//
		this.scrollImgTexture = RES.getRes("stage01");
		this.scrollImgTextureWidth = this.scrollImgTexture.textureWidth;
		this.scrollImgtextureHeight = this.scrollImgTexture.textureHeight;
		//
		this.imgCount = Math.ceil(this.stageH / this.scrollImgtextureHeight) + 1;
		//
		this.arrScrollImg = [];
		for (var i = 0; i < this.imgCount; i++) {
			var img:egret.Bitmap = this.createBitmapByName("stage01");
			img.y = this.stageH - this.scrollImgtextureHeight * this.imgCount + this.scrollImgtextureHeight * i;
			this.arrScrollImg.push(img);
			this.addChild(img);
		}

		

	}

	public stageScrollStart() {
		this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	public stageScrollPause() {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	private onEnterFrame(et: egret.Event) {
		for (var i = 0; i < this.imgCount; i++) {
			var img: egret.Bitmap = this.arrScrollImg[i];
			img.y += this.stageScrollSpeed;
			if (img.y > this.stageH) {
				img.y = this.arrScrollImg[0].y - this.scrollImgtextureHeight;
				this.arrScrollImg.pop();
				this.arrScrollImg.unshift(img);
			}
		}
	}

	/**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
     */
    private createBitmapByName(name:string):egret.Bitmap {
        var result = new egret.Bitmap();
        var texture:egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }


}